package com.cskaoyan.controller.wxcontroller;

import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.beanwx.vo.SearchIndexVo;
import com.cskaoyan.exception.DataDisplayException;
import com.cskaoyan.service.SearchService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: lds
 * @clsss: SearchController
 */
@RestController
@RequestMapping("wx/search")
public class SearchController {
    @Autowired
    SearchService searchService;
    /**
     * @author: lds
     * @title: searchIndex
     * @description:
     * @param: []
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */

    @RequestMapping("index")
    public BaseRespVo searchIndex() throws DataDisplayException {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        SearchIndexVo searchIndexVo = searchService.queryKeyWords(user);
        if (searchIndexVo == null) {
            throw new DataDisplayException();
        }

        return BaseRespVo.ok(searchIndexVo);
    }

    @RequestMapping("helper")
    public BaseRespVo searchHelper(String keyword){
        List list = searchService.queryHelperKeyWords(keyword);
        return BaseRespVo.ok(list);
    }
    @PostMapping("clearhistory" )
    public BaseRespVo searchClearHistory() throws DataDisplayException {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Integer integer = searchService.removeHistory(user);
        if (integer==null||integer==404)
        {
            throw new DataDisplayException();
        }
        return BaseRespVo.ok();
    }

}
